/**
 * @Author: yuanmanxue
 * @Date:   2018-01-12 03:22:08
 * @Last modified by:   yuanmanxue
 * @Last modified time: 2018-01-15 03:19:46
 */

// {
//   process.nextTick(function(){
//     console.log('process nextTick 1');
//   });
//   process.nextTick(function(){
//     console.log('process nextTick 2');
//   });
//   setImmediate(function(){
//     console.log('setImmediate 1');
//     process.nextTick(function(){
//       console.log('process nextTick 强行插入 ');
//     });
//   });
//   setImmediate(function(){
//     console.log('setImmediate 2');
//   })
//   console.log('正常22222');
// }
{
  //数组扁平化
  //方法1
  let arr=[1,[2,[3,[4,5],6],7],8];
  function flat1(arr) {
      while (arr.some(item => Array.isArray(item))) {
          arr = [].concat(...arr);
      }
      return arr;
  }
  // console.log(flat1(arr))
  // 方法2
  let flat2=function* (a) {
      var len=a.length;
      let ret = [];
      for (var i = 0; i < len; i++) {
            if (Array.isArray(a[i]))
            yield* flat2(a[i])
            else yield a[i]
          }
  }
    let ret = [];
  for(let item of flat2(arr)){
    ret.push(item);
  }
  // console.log(ret);
  // 方法3
  function flat3(arr){
    let ret = arr.toString().split(',');
    console.log(ret);
  }
  // flat3(arr)
}
{
  // 冒泡排列
  function Bubble(a){
    for(var i=0;i<=a.length-1;i++){
      // console.log(i);
      for(var j=0;j<=a.length-i;j++){
        // console.log(j);
        if(a[j]>a[j+1]){
          var tmp;
          tmp=a[j];
          a[j]=a[j+1];
          a[j+1]=tmp;
        }
      }
    }
      return a
  }
  let arrt=[1,5,6,8,9,2,6,85,2,3];
  // console.log(Bubble(arrt));
  // 快速排列
  function quickSort(arr){
    if(arr.length<=1){
      return arr;
    }
    var centerIndex = Math.floor(arr.length / 2);
    var centeritem = arr.splice(centerIndex,1)[0];
    var left=[];
    var right=[];
    for(var i=0;i<arr.length;i++){
      if(arr[i]<centeritem){
        left.push(arr[i]);
      }else{
        right.push(arr[i]);
      }
    }
    return quickSort(left).concat([centeritem],quickSort(right))
  }
  // console.log(quickSort(arrt));
  
}
